package com.ruoyi.worker.service.impl;


import com.ruoyi.worker.domain.BusinessVolume;
import com.ruoyi.worker.domain.StrategyItem;
import com.ruoyi.worker.domain.StrategyMain;
import com.ruoyi.worker.domain.StrategyPage;
import com.ruoyi.worker.mapper.StrategyConfigMapper;
import com.ruoyi.worker.service.IStrategyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: StrategyConfigServiceImpl
 * @Description:
 * @Author: 刘松山
 * @Date: 2021/10/11 11:01
 */
@Service
public class StrategyConfigServiceImpl implements IStrategyConfigService {

    @Autowired
    private StrategyConfigMapper strategyConfigMapper;

    @Override
    public List<StrategyMain> listStrategyMain() {
        return strategyConfigMapper.listStrategyMain();
    }

    @Override
    public List<StrategyItem> listStrategyItem() {
        return strategyConfigMapper.listStrategyItem();
    }

    @Override
    public List<BusinessVolume> ListBusinessVolume() {
        return strategyConfigMapper.ListBusinessVolume();
    }

    private int SIZE = 6;

    @Override
    @Transactional
    public int addStrategyItem(StrategyPage strategyPage) {

        List<StrategyItem> list = new ArrayList<>();
        List<StrategyMain> mainList = strategyConfigMapper.listStrategyMain();
        List<Integer> intList = new ArrayList<>();
        intList.add(strategyPage.getStrategy1());
        intList.add(strategyPage.getStrategy2());
        intList.add(strategyPage.getStrategy3());


        for (int i = 0; i < mainList.size(); i++) {
            StrategyMain m = mainList.get(i);
            StrategyItem item = new StrategyItem();
            item.setStrategyCode(m.getStrategyCode());
            item.setStrategyValue(intList.get(i));
            item.setCreateBy(strategyPage.getCreateBy());
            list.add(item);
        }

        int flag = strategyConfigMapper.delStrategyItem();

        flag = strategyConfigMapper.addStrategyItem(list);
        flag = strategyConfigMapper.delBusinessVolume();
        List<BusinessVolume> businessVolumeLis = new ArrayList<>();
        List<Double> doubleList = new ArrayList<>();
        if (strategyPage.getStrategy3() == 2) {

            BusinessVolume businessVolume = new BusinessVolume();
            businessVolume.setBusinessVolumeName("统一比率");
            businessVolume.setBusinessVolumeType(1);
            businessVolume.setBusinessVolumeValue(Double.parseDouble(strategyPage.getBusinessVolumeValue()));
            businessVolume.setCreateBy(strategyPage.getCreateBy());
            businessVolumeLis.add(businessVolume);
        } else if (strategyPage.getStrategy3() == 3) {
            String[] value = strategyPage.getValue().split(";");
            String[] valueName = strategyPage.getValueName().split(";");
            String[] businessId = strategyPage.getBusinessId().split(";");
            for (int i = 0; i < value.length; i++) {
                BusinessVolume businessVolume = new BusinessVolume();
                businessVolume.setBusinessVolumeName(valueName[i]);
                businessVolume.setBusinessId(Long.parseLong(businessId[i]));
                businessVolume.setBusinessVolumeType(2);
                businessVolume.setBusinessVolumeValue(Double.parseDouble(value[i]));
                businessVolume.setCreateBy(strategyPage.getCreateBy());
                businessVolume.setSort(i+1);

                businessVolumeLis.add(businessVolume);
            }
        }

        if (businessVolumeLis.size() > 0) {
            flag = strategyConfigMapper.addBusinessVolume(businessVolumeLis);
        }



        return 1;
    }

    @Override
    public List<Map<String, Object>> ListSchemeConfiguration() {
        List<Map<String, Object>> list =strategyConfigMapper.ListSchemeConfiguration();
        List<Map<String, Object>> resultList = new ArrayList<>();
        int  i=0;
        for(Map<String, Object> m:list ){
            i++;
            Map<String, Object> map = new HashMap<>();
            map.put("value","value"+i);
            map.put("lable", m.get("business_classification"));
            map.put("businessId", m.get("id"));
            resultList.add(map);
        }
        return resultList;
    }


}
